﻿

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using DAL;
using BOL;
using Website.Logger;


namespace UILGestion.Admin
{
    public partial class EmpresaReadForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    
                    internalid.Value = (string)Request.QueryString["id"];
                    menuCtrl1.loadMenuSeguridad();
                    FormatForm();
                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);

                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }

            }
        }

        private void readObject()
        {
            dbGestionDataContext _db =
                new dbGestionDataContext(Parametros.strConnString);



             Empresa empresa=
            
                _db.Empresas.Single(p => p.idInternal == int.Parse(internalid.Value));


             codigoLabel.Text = empresa.codigo;
             descripcionLabel.Text = empresa.descripcion;
             direccionPagoLabel.Text = empresa.direccion;
            razonSocialLabel.Text=empresa.razonSocial;
rucLabel.Text=empresa.ruc;
ActivoCheckBox.Checked= empresa.activo.Value;
           
        }
        private void FormatForm()
        {



            //ShowAllComboBox();
            //restricionesForm();



            string Lang = "es-ES";  //para controlar el idioma 
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo
   (Lang);

            readObject();


        }




        protected void AccionButton_Click(object sender, EventArgs e)
        {
            try
            {
                if ((sender as Button).ID == "refreshButton")
                {
                    Response.Redirect("EmpresaReadForm.aspx?id=" + internalid.Value, false);
                }
                if ((sender as Button).ID == "editarButton")
                {
                    Response.Redirect("EmpresaEditForm.aspx?id=" + internalid.Value,false);
                    
                }
                if ((sender as Button).ID == "nuevoButton")
                {
                    Response.Redirect("EmpresaInsertForm.aspx", true);
                }
                if ((sender as Button).ID == "eliminarButton")
                {

                    EmpresaBL empresa = new EmpresaBL();
                    empresa.delete(int.Parse(internalid.Value));
                    
                    Response.Redirect("EmpresaListForm.aspx", true);
                }
              
                if ((sender as Button).ID == "anteriorButton")
                {

                    dbGestionDataContext _db = 
                        new dbGestionDataContext(Parametros.strConnString);

                    Empresa empresa = new Empresa();

                    int idAnterior = empresa.getidAnterior(_db, int.Parse(internalid.Value));
                    Response.Redirect("EmpresaReadForm.aspx?id=" + idAnterior, false);

                  
                }
                if ((sender as Button).ID == "siguienteButton")
                {

                    dbGestionDataContext _db =
                       new dbGestionDataContext(Parametros.strConnString);

                    Empresa empresa = new Empresa();

                    int idSiguiente = empresa.getidSiguiente(_db, int.Parse(internalid.Value));
                    Response.Redirect("EmpresaReadForm.aspx?id=" + idSiguiente, false);
                }



            }

            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }



        }



        


     
    }
}